<?php


require_once(DIR_LIB."vslibdef.php");

if(!defined('DIR_LIB_VSCORE'))
	define('DIR_LIB_VSCORE', DIR_LIB."VsCore".DIR_SEP);

if(!defined('DIR_LIB_VSSHOP'))
	define('DIR_LIB_VSSHOP', DIR_LIB."VsShop".DIR_SEP);

require_once(DIR_LIB_VSCORE."vsapplication.php");
require_once(DIR_LIB_VSCORE."vssession.php");

require_once(DIR_LIB_VSSHOP."vsorder.php");


class VsShoppingCart {

	protected static $_Instance = NULL;	// Single Instance of this CLASS

	protected static $_session = NULL;

	protected $_order = NULL;

	//protected $_cartItems = array();

	public static function Instance() {
		if(!self::$_session)
			self::$_session = VsSession::Instance();

		if(self::$_session->isExists('VsShoppingCart'))
			self::$_Instance = self::unserializeCart();

		if(!self::$_Instance)
			self::$_Instance = &new VsShoppingCart();

		return self::$_Instance;
	}
	//==========================================================================================

	function order() { return $this->_order; }

	function items() { return $this->_order ? $this->_order->items() : array(); }
	//==========================================================================================

	function addItem($productId, $qty) {
		if(!$this->_order)
			return FALSE;

		return $this->_order->addItem($productId, $qty);
	}
	//=========================================================================================

	function removeItem($id) {
		$this->_order->removeItem($id);
	}
	//=========================================================================================

	function addToCart($productId, $qty) {
		$this->addItem($productId, $qty);
	}
	//==========================================================================================

	function isEmpty() {
		return sizeof($this->items()) ? FALSE : TRUE;
	}
	//=========================================================================================

	function saveOrder() {
		$this->_order->save();
	}
	//=========================================================================================

	function showCart($type='html') {
		if($type=='html')
			$this->showCartHtml();
		elseif($type=='xml')
			$this->showCartXml();
	}
	//==========================================================================================
	
	protected function showCartHtml() {
		$html = "<table><tr>";
		$html .= "<th colspan=\"2\" align=\"center\"><h2>VsShoppingCart</h2></th>";
	
		if(sizeof($this->_cartItems)) {
			foreach($this->_cartItems as $ci) {
				$html .= "</tr><tr>";
				$html .= "<td>".$ci->productName()."</td>";
				$html .= "<td>".$ci->qty()."</td>";
			}
		}
		else {
			$html .= "</tr><tr>";
			$html .= "<td colspan=\"2\" align=\"center\">Cart is Empty</td>";
		}

		$html .= "</tr></table>";

		echo $html;
	}
	//==========================================================================================

	protected function showCartXml() {

	}
	//==========================================================================================

	protected function setOrder($order) {

		$this->_order = $order;
	}
	//===========================================================================================

	protected function __construct() {
		//echo "VsShoppingCart Constructor";

		$this->setOrder(new VsOrder());
	}
	//===========================================================================================

	function __destruct() {
		$this->serializeCart();
	}
	//============================================================================================

	protected function serializeCart() {
		self::$_session->assign('VsShoppingCart', serialize($this));
	}
	//==========================================================================================

	protected static function unserializeCart() {
		$cart = unserialize(self::$_session->value('VsShoppingCart'));

		return $cart;
	}
	//============================================================================================

};

?>
